package c.j.a.c;

import android.os.Build.VERSION;
import c.j.a.a.a.c.d;
import c.j.a.c.g$g.d.c;
import c.j.a.c.g.y;
import c.j.a.d.a.j.e;
import c.j.a.d.b.n.d0;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: DownloadComponentManager */
public class o {
    public ExecutorService a;
    public ExecutorService b;
    public ScheduledExecutorService c;

    /* compiled from: DownloadComponentManager */
    public static class a {
        public static o a = new o(null);
    }

    /* compiled from: AdEventHandler */
    public class b {

        /* compiled from: AdEventHandler */
        public static class b {
            public static b a = new b(null);
        }

        public b(a aVar) {
        }

        public final JSONObject a(d dVar) {
            JSONObject jSONObject = new JSONObject();
            try {
                c.j.a.c.q.d.g(dVar.F(), jSONObject);
                jSONObject.putOpt("download_url", dVar.a());
                jSONObject.putOpt("package_name", dVar.v());
                jSONObject.putOpt("android_int", Integer.valueOf(VERSION.SDK_INT));
                jSONObject.putOpt("rom_name", d0.g());
                jSONObject.putOpt("rom_version", d0.h());
                jSONObject.putOpt("ttdownloader", Integer.valueOf(1));
            } catch (Exception unused) {
            }
            return jSONObject;
        }

        public final JSONObject b(c.j.a.b.a.c.a aVar) {
            JSONObject jSONObject = new JSONObject();
            try {
                c.j.a.c.q.d.g(aVar.p, jSONObject);
                jSONObject.putOpt("download_url", aVar.f);
                jSONObject.putOpt("package_name", aVar.e);
                jSONObject.putOpt("android_int", Integer.valueOf(VERSION.SDK_INT));
                jSONObject.putOpt("rom_name", d0.g());
                jSONObject.putOpt("rom_version", d0.h());
                jSONObject.putOpt("ttdownloader", Integer.valueOf(1));
            } catch (Exception unused) {
            }
            return jSONObject;
        }

        public void c(long j, int i) {
            c.j.a.c.g$g.d.b h = c.a.h(j);
            if (h.a()) {
                c.j.a.c.q.d.n();
            } else if (h.c.l()) {
                int i2 = 1;
                c.j.a.a.a.c.c cVar = h.c;
                String c = i == 1 ? cVar.c() : cVar.b();
                String f = c.j.a.c.q.d.f(h.c.d(), "click");
                JSONObject jSONObject = new JSONObject();
                try {
                    jSONObject.putOpt("click_type", Integer.valueOf(i));
                    jSONObject.putOpt("permission_notification", Integer.valueOf(e.a() ? 1 : 2));
                    String str = "network_available";
                    if (!c.j.a.d.b.n.a.G(y.a())) {
                        i2 = 2;
                    }
                    jSONObject.putOpt(str, Integer.valueOf(i2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                m(c, f, jSONObject, h.b, h.c);
            }
        }

        public void d(long j, int i, com.ss.android.socialbase.downloader.g.c cVar) {
            c.j.a.c.g$g.d.b h = c.a.h(j);
            if (h.a()) {
                c.j.a.c.q.d.n();
                return;
            }
            String str = null;
            JSONObject jSONObject = new JSONObject();
            int i2 = 2;
            c.j.a.b.a.c.a b;
            if (i == 1) {
                str = c.j.a.c.q.d.f(h.c.i(), "storage_deny");
            } else if (i == 2) {
                str = c.j.a.c.q.d.f(h.c.e(), "click_start");
                try {
                    f(cVar, jSONObject);
                    b = c.a.b(cVar);
                    if (b != null) {
                        String str2 = "is_update_download";
                        if (b.A) {
                            i2 = 1;
                        }
                        jSONObject.put(str2, i2);
                        y.e(b, jSONObject);
                    }
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            } else if (i == 3) {
                str = c.j.a.c.q.d.f(h.c.f(), "click_pause");
                b = c.a.b(cVar);
                if (b != null) {
                    try {
                        f(cVar, jSONObject);
                        jSONObject.putOpt("time_after_click", Long.valueOf(System.currentTimeMillis() - b.O));
                        jSONObject.putOpt("click_download_size", Long.valueOf(b.P));
                        jSONObject.putOpt("download_length", Long.valueOf(cVar.I()));
                        jSONObject.putOpt("download_apk_size", Long.valueOf(cVar.T));
                        synchronized (b) {
                            b.t++;
                        }
                        c.j.a.c.g$g.g.b.a.a(b);
                        jSONObject.put("click_pause_times", b.t);
                        long j2 = cVar.T;
                        long I = cVar.I();
                        double d = (I < 0 || j2 <= 0) ? 0.0d : ((double) I) / ((double) j2);
                        jSONObject.put("download_percent", d);
                        jSONObject.put("download_status", cVar.p0());
                        j2 = System.currentTimeMillis();
                        I = b.o;
                        if (I > 0) {
                            jSONObject.put("time_from_start_download", j2 - I);
                        }
                        I = b.e();
                        if (I > 0) {
                            jSONObject.put("time_from_download_resume", j2 - I);
                        }
                        jSONObject.putOpt("fail_status", Integer.valueOf(b.y));
                        jSONObject.putOpt("fail_msg", b.z);
                        jSONObject.put("download_failed_times", b.s);
                        String str3 = "can_show_notification";
                        if (e.a()) {
                            i2 = 1;
                        }
                        jSONObject.put(str3, i2);
                        cVar.M();
                        jSONObject.put("first_speed_time", cVar.D0.optLong("dbjson_key_first_speed_time"));
                        jSONObject.put("all_connect_time", cVar.a());
                        jSONObject.put("download_prepare_time", cVar.b());
                        jSONObject.put("download_time", (cVar.A() + cVar.a()) + cVar.b());
                    } catch (Throwable th2) {
                        th2.printStackTrace();
                    }
                }
            } else if (i == 4) {
                str = c.j.a.c.q.d.f(h.c.g(), "click_continue");
                f(cVar, jSONObject);
            } else if (i == 5) {
                if (cVar != null) {
                    try {
                        y.f(jSONObject, cVar.g0());
                        g.g(jSONObject, cVar);
                    } catch (Throwable unused) {
                    }
                }
                str = c.j.a.c.q.d.f(h.c.h(), "click_install");
            }
            l(h.c.b(), str, jSONObject, h.b.g(), 1, h.b, h.c);
        }

        public void e(long j, boolean z, int i) {
            c.j.a.c.g$g.d.b h = c.a.h(j);
            if (h.a()) {
                c.j.a.c.q.d.n();
            } else if (h.b.E() != null) {
                JSONObject jSONObject = new JSONObject();
                try {
                    jSONObject.putOpt("click_type", Integer.valueOf(i));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                m(h.c.b(), z ? "deeplink_quickapp_success" : "deeplink_quickapp_failed", jSONObject, h.b, h.c);
            }
        }

        public void f(com.ss.android.socialbase.downloader.g.c cVar, JSONObject jSONObject) {
            if (cVar != null) {
                try {
                    jSONObject.putOpt("total_bytes", Long.valueOf(cVar.T));
                    jSONObject.putOpt("cur_bytes", Long.valueOf(cVar.I()));
                    jSONObject.putOpt("chunk_count", Integer.valueOf(cVar.R));
                    jSONObject.putOpt("app_name", cVar.j0());
                    jSONObject.putOpt("network_quality", cVar.O);
                    jSONObject.putOpt("save_path", cVar.e);
                    c.j.a.c.g$g.d dVar = c.a;
                    int g0 = cVar.g0();
                    for (c.j.a.b.a.c.a aVar : dVar.e.values()) {
                        if (aVar != null) {
                            if (aVar.q == g0) {
                                break;
                            }
                        }
                    }
                    c.j.a.b.a.c.a aVar2 = null;
                    if (aVar2 != null) {
                        jSONObject.putOpt("click_download_time", Long.valueOf(aVar2.O));
                        jSONObject.putOpt("click_download_size", Long.valueOf(aVar2.P));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
            }
            int i = 1;
            jSONObject.putOpt("permission_notification", Integer.valueOf(e.a() ? 1 : 2));
            jSONObject.putOpt("network_available", Integer.valueOf(c.j.a.d.b.n.a.G(y.a()) ? 1 : 2));
            String str = "network_is_wifi";
            if (!c.j.a.d.b.n.a.t(y.a())) {
                i = 2;
            }
            jSONObject.putOpt(str, Integer.valueOf(i));
        }

        public void g(String str, long j) {
            c.j.a.b.a.c.a aVar = (c.j.a.b.a.c.a) c.a.e.get(Long.valueOf(j));
            if (aVar == null) {
                c.j.a.c.q.d.n();
            } else {
                q(aVar.D, str, null, aVar);
            }
        }

        public void h(String str, d dVar, c.j.a.a.a.c.c cVar) {
            m(cVar.b(), str, dVar.F(), dVar, cVar);
        }

        public void i(String str, c.j.a.b.a.c.a aVar) {
            if (aVar == null) {
                c.j.a.c.q.d.n();
            } else {
                q(aVar.D, str, null, aVar);
            }
        }

        public void j(String str, c.j.a.c.g$g.d.b bVar) {
            m(bVar.c.b(), str, bVar.b.F(), bVar.b, bVar.c);
        }

        public void k(String str, String str2, c.j.a.b.a.c.a aVar) {
            q(str, str2, null, aVar);
        }

        public final void l(String str, String str2, JSONObject jSONObject, long j, int i, d dVar, c.j.a.a.a.c.c cVar) {
            try {
                c.j.a.a.a.e.d.a aVar = new c.j.a.a.a.e.d.a();
                aVar.b = c.j.a.c.q.d.f(str, "embeded_ad");
                aVar.c = str2;
                aVar.d = dVar.t();
                aVar.e = dVar.d();
                aVar.f = dVar.u();
                aVar.g = j;
                aVar.l = cVar.a();
                aVar.i = dVar.C();
                aVar.h = c.j.a.c.q.d.h(a(dVar), cVar.n(), jSONObject);
                aVar.k = cVar.j();
                aVar.j = i;
                aVar.m = cVar.m();
                c.j.a.a.a.e.d a = aVar.a();
                c.j.a.a.a.a.c cVar2 = y.b;
                if (cVar2 != null) {
                    if (a.l) {
                        cVar2.a(a);
                    } else {
                        cVar2.b(a);
                    }
                }
            } catch (Exception e) {
                c.j.a.c.q.d.j(e);
            }
        }

        public final void m(String str, String str2, JSONObject jSONObject, d dVar, c.j.a.a.a.c.c cVar) {
            l(str, str2, jSONObject, dVar.g(), 2, dVar, cVar);
        }

        public void n(String str, JSONObject jSONObject, c.j.a.b.a.c.a aVar) {
            if (aVar == null) {
                c.j.a.c.q.d.n();
            } else {
                q("embeded_ad", str, jSONObject, aVar);
            }
        }

        public void o(String str, JSONObject jSONObject, c.j.a.c.g$g.d.b bVar) {
            m(bVar.c.b(), str, jSONObject, bVar.b, bVar.c);
        }

        public void p(com.ss.android.socialbase.downloader.g.c cVar, com.ss.android.socialbase.downloader.e.a aVar) {
            c.j.a.b.a.c.a b = c.a.b(cVar);
            if (b == null) {
                c.j.a.c.q.d.n();
            } else if (!b.J.get()) {
                JSONObject jSONObject = new JSONObject();
                try {
                    int i;
                    int i2;
                    f(cVar, jSONObject);
                    g.n(jSONObject, cVar);
                    if (aVar != null) {
                        jSONObject.putOpt("fail_status", Integer.valueOf(aVar.a));
                        jSONObject.putOpt("fail_msg", aVar.b);
                        b.y = aVar.a;
                        b.z = aVar.b;
                    }
                    synchronized (b) {
                        i = 1;
                        i2 = b.s + 1;
                        b.s = i2;
                    }
                    jSONObject.put("download_failed_times", i2);
                    if (cVar.T > 0) {
                        jSONObject.put("download_percent", ((double) cVar.I()) / ((double) cVar.T));
                    }
                    jSONObject.put("has_send_download_failed_finally", b.K.get() ? 1 : 2);
                    y.e(b, jSONObject);
                    String str = "is_update_download";
                    if (!b.A) {
                        i = 2;
                    }
                    jSONObject.put(str, i);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                q(b.D, "download_failed", jSONObject, b);
                c.j.a.c.g$g.g.b.a.a(b);
            }
        }

        public final void q(String str, String str2, JSONObject jSONObject, c.j.a.b.a.c.a aVar) {
            try {
                c.j.a.a.a.e.d.a aVar2 = new c.j.a.a.a.e.d.a();
                aVar2.b = c.j.a.c.q.d.f(str, "embeded_ad");
                aVar2.c = str2;
                aVar2.d = aVar.n;
                aVar2.e = aVar.a;
                aVar2.f = aVar.c;
                aVar2.g = aVar.b;
                aVar2.l = aVar.E;
                aVar2.h = c.j.a.c.q.d.h(b(aVar), jSONObject);
                aVar2.j = 2;
                aVar2.m = aVar.F;
                c.j.a.a.a.e.d a = aVar2.a();
                c.j.a.a.a.a.c cVar = y.b;
                if (cVar != null) {
                    if (a.l) {
                        cVar.a(a);
                    } else {
                        cVar.b(a);
                    }
                }
            } catch (Exception e) {
                c.j.a.c.q.d.j(e);
            }
        }
    }

    public void a(Runnable runnable, long j) {
        try {
            d().schedule(runnable, j, TimeUnit.MILLISECONDS);
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    public void b(Runnable runnable, boolean z) {
        if (runnable != null) {
            if (!z || c.j.a.c.q.d.k()) {
                if (this.a == null) {
                    synchronized (o.class) {
                        if (this.a == null) {
                            TimeUnit timeUnit = TimeUnit.SECONDS;
                            SynchronousQueue synchronousQueue = new SynchronousQueue();
                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.append(z.class.getName());
                            stringBuilder.append("-CPUThreadPool");
                            this.a = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 30, timeUnit, synchronousQueue, new c.j.a.d.b.m.a(stringBuilder.toString()));
                        }
                    }
                }
                this.a.execute(runnable);
            } else {
                runnable.run();
            }
        }
    }

    public void c(Runnable runnable) {
        if (this.b == null) {
            synchronized (o.class) {
                if (this.b == null) {
                    TimeUnit timeUnit = TimeUnit.SECONDS;
                    SynchronousQueue synchronousQueue = new SynchronousQueue();
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(z.class.getName());
                    stringBuilder.append("-IOThreadPool");
                    this.b = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 30, timeUnit, synchronousQueue, new c.j.a.d.b.m.a(stringBuilder.toString()));
                }
            }
        }
        this.b.execute(runnable);
    }

    public ScheduledExecutorService d() {
        if (this.c == null) {
            synchronized (o.class) {
                if (this.c == null) {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(z.class.getName());
                    stringBuilder.append("-ScheduledThreadPool");
                    this.c = new ScheduledThreadPoolExecutor(0, new c.j.a.d.b.m.a(stringBuilder.toString()));
                }
            }
        }
        return this.c;
    }

    public o(n nVar) {
    }
}
